﻿using System;
using System.Collections.Generic;

namespace Mp3Observer.Common.IoC
{
    /// <summary>
    /// the key of the dictionary is the type and the name.
    /// </summary>
    public struct DependencyItemKey : IEquatable<DependencyItemKey>
    {
        /// <summary>
        /// Gets or sets the type of the contract.
        /// </summary>
        /// <value>The type of the contract.</value>
        public Type ContractType { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">The left person.</param>
        /// <param name="right">The right person.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(DependencyItemKey left, DependencyItemKey right)
        {
            if (Object.ReferenceEquals(left, null))
                return Object.ReferenceEquals(right, null);
            return left.Equals(right);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">The left person.</param>
        /// <param name="right">The right person.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(DependencyItemKey left, DependencyItemKey right)
        {
            if (Object.ReferenceEquals(left, null))
                return !Object.ReferenceEquals(right, null);
            return !left.Equals(right);
        }
        
		public override bool Equals(object obj)
		{
			var person = (DependencyItemKey)obj; // if this is a struct: = (DependencyItemKey)obj
			return person == null ? false : this.Equals(person);
		}

        public bool Equals(DependencyItemKey other)
		{
			if (Object.ReferenceEquals(other, null))
				return false;
			if (Object.ReferenceEquals(other, this))
				return true;
             // return ((this.ContractType == other.ContractType) && (this.Name == other.Name));
            return EqualityComparer<Type>.Default.Equals(this.ContractType, other.ContractType) &&
                EqualityComparer<string>.Default.Equals(this.Name, other.Name);
		}

        public override int GetHashCode()
		{
			int hashCode = 0;
			if (this.ContractType != null) hashCode ^= this.ContractType.GetHashCode();
			if (this.Name != null) hashCode ^= this.Name.GetHashCode();
			return hashCode;
		}
    }
}
